﻿using Abp.Domain.Uow;
using Microsoft.AspNetCore.Mvc;
using Mt.Site.Application.SEO;
using Mt.Site.Application.SEO.Dto;
using Mt.Site.Web.Models.Layout;
using Mt.Site.Web.Views;
using System.Threading.Tasks;

namespace Mt.Site.Web.Components
{
    public class SeoViewComponent : MtSiteFrontEndViewComponent
    {
        private readonly ISeoFrontEndAppService _seoFrontEndAppService;
        private readonly IUnitOfWorkManager _unitOfWorkManager;

        public SeoViewComponent(
            ISeoFrontEndAppService seoFrontEndAppService,
            IUnitOfWorkManager unitOfWorkManager)
        {
            _seoFrontEndAppService = seoFrontEndAppService;
            _unitOfWorkManager = unitOfWorkManager;
        }

        public async Task<IViewComponentResult> InvokeAsync()
        {
            var RelatedId = ViewBag.RelatedId;
            var DefaultTitle= ViewBag.DefaultTitle;

            var dto = await _unitOfWorkManager.WithUnitOfWorkAsync(async () => {
                return (await _seoFrontEndAppService.GetSeo(new GetSeoInput()
                {
                    RelatedId = RelatedId,
                    DefaultTitle = DefaultTitle
                }));
            });

            var seoModel = ObjectMapper.Map<SeoModel>(dto);

            return View("~/Views/Layout/_Seo.cshtml", seoModel);
        }
    }
}